package com.shizhanzhe.zxbapp.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.shizhanzhe.zxbapp.MainActivity;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.vip.PDDFragment;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2019/3/11.
 */
public class WelcomeGuideActivity extends Activity {

    private Banner pager;
    private View btn;
    private List<View> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_guide);
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 5.0 以上全透明状态栏
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏 加下面几句可以去除透明状态栏的灰色阴影,实现纯透明
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            //6.0 以上可以设置状态栏的字体为黑色.使用下面注释的这行打开亮色状态栏模式,实现黑色字体,白底的需求用这句setStatusBarColor(Color.WHITE);
            window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 全透明状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        pager = findViewById(R.id.welcome_guide_viewpager);
        Integer[] images={R.mipmap.w1,R.mipmap.w2,R.mipmap.w3,R.mipmap.w4,R.mipmap.w5};
        List<Integer> resultList = new ArrayList<>(images.length);
        for (Integer s : images) {
            resultList.add(s);
        }
        pager.setImages(resultList).setImageLoader(new GlideImageLoader()).start();
        btn = findViewById(R.id.welcome_guide_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn.setEnabled(false);
                startActivity(new Intent(WelcomeGuideActivity.this, MainActivity.class));
                finish();
            }
        });
//        initViewPager();
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 4)
                {
                    btn.setVisibility(View.VISIBLE);
                } else {
                    btn.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        pager.isAutoPlay(false);
    }
    class GlideImageLoader extends ImageLoader

    {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }
//    public void initViewPager() {
//        list = new ArrayList<View>();
//        ImageView iv = new ImageView(this);
//        iv.setImageResource(R.mipmap.w1);
//        iv.setScaleType(ImageView.ScaleType.FIT_XY);
//        list.add(iv);
//        ImageView iv1 = new ImageView(this);
//        iv1.setImageResource(R.mipmap.w2);
//        iv1.setScaleType(ImageView.ScaleType.FIT_XY);
//        list.add(iv1);
//        ImageView iv2 = new ImageView(this);
//        iv2.setImageResource(R.mipmap.w3);
//        iv2.setScaleType(ImageView.ScaleType.FIT_XY);
//        list.add(iv2);
//        ImageView iv3 = new ImageView(this);
//        iv3.setImageResource(R.mipmap.w4);
//        iv3.setScaleType(ImageView.ScaleType.FIT_XY);
//        list.add(iv3);
//        ImageView iv4 = new ImageView(this);
//        iv4.setImageResource(R.mipmap.w5);
//        iv4.setScaleType(ImageView.ScaleType.FIT_XY);
//        list.add(iv4);
//        pager.setAdapter(new MyViewPagerAdapter());
//
//        //监听滑动效果
//        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            //被选中的方法
//            @Override
//            public void onPageSelected(int arg0) {
//                // TODO Auto-generated method stub
//                if (arg0 == 4)//如果是第三个页面
//                {
//                    btn.setVisibility(View.VISIBLE);
//
//                } else {
//                    btn.setVisibility(View.GONE);
//
//                }
//            }
//
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//                // TODO Auto-generated method stub
//
//            }
//        });
//    }

    //自定义viewpager的适配器
    class MyViewPagerAdapter extends PagerAdapter {


        //计算需要多少item显示
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        //初始化item实例方法
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        //item的销毁的方法
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            //必须注销掉父类的方法
            //super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }

    }

}
